package com.sfx.算法专题.位运算;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-08-21
 * Time: 11:10
 */
public class Code_7_getSUM {
    static class Solution {
        public int getSum(int a, int b) {
            //a:        0  0  1  1  0  1
            //b:        0  1  1  1  0  0
            //a^b =     0  1  0  0  0  1  a
            //(a&b)<<1  0  1  1  0  0  0  b
            //a^b =     0  0  1  0  0  1  a
            //(a&b)<<1  1  0  0  0  0  0  b
            //a^b =     1  0  1  0  0  1  a
            //(a&b)<<1  0  0  0  0  0  0  b
            // 当b=0的时候停止,返回a
            while(b!=0) {
                int x = a^b; //计算无进位相加结果
                int carry = (a & b)<<1;//计算进位
                a  = x;
                b = carry;
            }
            return a;
        }
    }
}